<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'bas:car:basCar:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleChangeStatus({ ope: CarOpe.WEIXIU })"
          v-auth="'bas:car:basCar:edit'"
        >
          <Icon /> {{ t('车辆维修') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleChangeStatus({ ope: CarOpe.OVER })"
          v-auth="'bas:car:basCar:edit'"
        >
          <Icon /> {{ t('维修完成') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleChangeStatus({ ope: CarOpe.BAOFEI })"
          v-auth="'bas:car:basCar:edit'"
        >
          <Icon /> {{ t('车辆报废') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleChangeStatus({ ope: CarOpe.CANCEL })"
          v-auth="'bas:car:basCar:edit'"
        >
          <Icon /> {{ t('取消报废') }}
        </a-button>
        <a-button type="primary" @click="handleChangeDriver({})" v-auth="'bas:car:basCar:edit'">
          <Icon /> {{ t('变更驾驶员') }}
        </a-button>
        <a-button type="primary" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="primary" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.cphm }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BasicModal v-bind="$attrs" @register="register" title="变更驾驶员" @ok="handleDriver">
      <BasicForm @register="registerForm" />
    </BasicModal>
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsBasCarBasCarList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basCarDelete, basCarListData } from '/@/api/bas/car/basCar';
  import {
    basCarDisable,
    basCarEnable,
    basCarUpdateStatus,
    basCarUpdateDriver,
  } from '/@/api/bas/car/basCar';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { CarOpe } from '/@/enums/GlobEnum';
  import { BasicModal } from '/@/components/Modal';
  import { useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('bas.car.basCar');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('车辆档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('车牌号码'),
        field: 'cphm',
        component: 'Input',
      },
      {
        label: t('品牌型号'),
        field: 'ppxh',
        component: 'Input',
      },
      {
        label: t('车辆类型'),
        field: 'cllx',
        component: 'Select',
        componentProps: {
          dictType: 'car_type',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('使用性质'),
        field: 'syxz',
        component: 'Select',
        componentProps: {
          dictType: 'car_use_type',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('默认驾驶员'),
        field: 'driver.userName',
        component: 'Input',
      },
      {
        label: t('车辆状态'),
        field: 'clzt',
        component: 'Select',
        componentProps: {
          dictType: 'car_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('车辆状态'),
      dataIndex: 'clzt',
      dictType: 'car_status',
      key: 'a.clzt',
      sorter: true,
      width: 100,
      align: 'center',
      fixed: 'left',
    },
    {
      title: t('车牌号码'),
      dataIndex: 'cphm',
      key: 'a.cphm',
      sorter: true,
      width: 100,
      align: 'left',
      fixed: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('品牌型号'),
      dataIndex: 'ppxh',
      key: 'a.ppxh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆类型'),
      dataIndex: 'cllx',
      key: 'a.cllx',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_type',
    },
    {
      title: t('使用性值'),
      dataIndex: 'syxz',
      key: 'a.syxz',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_use_type',
    },
    {
      title: t('车辆所有人'),
      dataIndex: 'clsyr',
      key: 'a.clsyr',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('座位数'),
      dataIndex: 'zws',
      key: 'a.zws',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('默认驾驶员'),
      dataIndex: 'driver.userName',
      key: 'a.mrjsy',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系电话'),
      dataIndex: 'lxdh',
      key: 'a.lxdh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发动机号码'),
      dataIndex: 'fdjhm',
      key: 'a.fdjhm',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆识别代号'),
      dataIndex: 'clsbdh',
      key: 'a.clsbdh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('新能源汽车'),
      dataIndex: 'xnyqc',
      key: 'a.xnyqc',
      dictType: 'sys_yes_no',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车体颜色'),
      dataIndex: 'ctys',
      key: 'a.ctys',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆配置'),
      dataIndex: 'clpz',
      key: 'a.clpz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('核定载人数'),
      dataIndex: 'hdzrs',
      key: 'a.hdzrs',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('整备质量(Kg)'),
      dataIndex: 'zbzl',
      key: 'a.zbzl',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('总质量'),
      dataIndex: 'zzl',
      key: 'a.zzl',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('变速器型式'),
      dataIndex: 'bsqxs',
      key: 'a.bsqxs',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发动机排量'),
      dataIndex: 'fdjpl',
      key: 'a.fdjpl',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('油箱容积'),
      dataIndex: 'yxrj',
      key: 'a.yxrj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('保养周期'),
      dataIndex: 'byzq',
      key: 'a.byzq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('销售单位'),
      dataIndex: 'xsdw',
      key: 'a.xsdw',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('购车时间'),
      dataIndex: 'gcsj',
      key: 'a.gcsj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('购车价格'),
      dataIndex: 'gcjg',
      key: 'a.gcjg',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('注册登记日期'),
      dataIndex: 'zcdjrq',
      key: 'a.zcdjrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('发证日期'),
      dataIndex: 'fzrq',
      key: 'a.fzrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('年检日期'),
      dataIndex: 'njrq',
      key: 'a.njrq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('保险到期'),
      dataIndex: 'bxdq',
      key: 'a.bxdq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('限行标志'),
      dataIndex: 'xxbz',
      key: 'a.xxbz',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('录入人员'),
      dataIndex: 'cldaLrry',
      key: 'a.clda_lrry',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('修改日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑车辆档案'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bas:car:basCar:edit',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用车辆档案'),
        popConfirm: {
          title: t('是否确认停用车辆档案'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'bas:car:basCar:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用车辆档案'),
        popConfirm: {
          title: t('是否确认启用车辆档案'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'bas:car:basCar:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除车辆档案'),
        popConfirm: {
          title: t('是否确认删除车辆档案'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'bas:car:basCar:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getSelectRowKeys, getForm }] = useTable({
    api: basCarListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('默认驾驶员'),
      field: 'mrjsy',
      component: 'ListSelect',
      componentProps: {
        selectType: 'empUserSelect',
        checkbox: false,
      },
      required: true,
    },
  ];

  const [register, { openModal, closeModal }] = useModal();
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/car/basCar/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDisable(record: Recordable) {
    const params = { id: record.id };
    const res = await basCarDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { id: record.id };
    const res = await basCarEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await basCarDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleChangeStatus(record) {
    if (getSelectRowKeys().length) {
      const res = await basCarUpdateStatus({
        selIds: getSelectRowKeys().join(','),
        ope: record.ope,
      });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择要修改的车辆！', 'error');
    }
  }

  // 驾驶员选择
  async function handleChangeDriver() {
    if (getSelectRowKeys().length) {
      openModal(true, {});
    } else {
      showMessage('请先选择要修改的车辆！', 'error');
    }
  }

  // 变更驾驶员
  async function handleDriver() {
    const data = await validate();
    const res = await basCarUpdateDriver({
      selIds: getSelectRowKeys().join(','),
      mrjsy: data.mrjsy,
    });
    showMessage(res.message);
    closeModal();
    handleSuccess({});
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
